package com.hnu.store.controller;

import com.hnu.store.dto.TradeDTO;
import com.hnu.store.entity.Trade;
import com.hnu.store.service.ITradeService;
import com.hnu.store.utils.JsonResult;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 交易的controller
 */
@RestController
@CrossOrigin

@RequestMapping("trade")
public class TradeController extends BaseController {

  @Autowired
  private ITradeService tradeService;

  /**
   * 根据用户id获取交易列表
   * @param userId 用户的id
   * @return 交易列表
   */
  @RequestMapping("getTradeListById")
  public JsonResult getTradeListById(Integer userId) {
    List<TradeDTO> recordList = tradeService.getTradeListById(userId);

    //  没有抛出异常
    JsonResult result = new JsonResult(OK);
    result.addData("tradeRecord", recordList);
    return result;
  }

  /**
   * 按照顺序获取交易列表
   * @param userId 用户id
   * @param sort 排序方式
   * @return 交易列表
   */
  @RequestMapping("getTradeListByIdOrder")
  public JsonResult getTradeListByIdOrder(Integer userId, String sort) {
    List<TradeDTO> tradeList = tradeService.getTradeListByIdOrder(userId, sort);
    //  没有抛出异常
    JsonResult result = new JsonResult(OK);
    result.addData("tradeList", tradeList);
    return result;
  }

  /**
   * 按作品名称查询
   * @param userId  用户id
   * @param stringPara  关键字
   * @param classification  分类
   * @param sort  排序方式
   * @return  返回trade
   */
  @RequestMapping("searchTradeByWorkName")
  public JsonResult searchTradeByWorkName(Integer userId, String stringPara, String classification,
      String sort) {
    //  获取dto列表
    List<TradeDTO> tradeList;
    if (sort.isEmpty()) {
      tradeList = tradeService.getTradeListById(userId);
    } else {
      tradeList = tradeService.getTradeListByIdOrder(userId, sort);
    }

    //  新建一个dto列表，筛去不符合条件的
    List<TradeDTO> recordList = new ArrayList<>();

    for (TradeDTO item : tradeList) {
      //  不匹配就跳过
      if (!item.getGoodsName().contains(stringPara)) {
        continue;
      }
      //  不符合筛选条件（作品分类）跳过
      if (!item.getGoodsType().equals(classification) && !classification.isEmpty()) {
        continue;
      }
      recordList.add(item);
    }
    JsonResult result = new JsonResult(OK);
    result.addData("tradeRecord", recordList);
    return result;
  }

  /**
   * 根据交易id获取交易
   * @param tradeId 交易id
   * @return 返回交易信息
   */
  @RequestMapping("getTradeById")
  public JsonResult getTradeById(Integer tradeId) {
    Trade trade = tradeService.getTradeById(tradeId);
    JsonResult result = new JsonResult(OK);
    result.addData("tradeInfo", trade);
    return result;
  }

  /**
   * 购买使用权同时生成交易凭证
   * @param trade  交易信息
   * @return json
   */
  @RequestMapping("buyUseRight")
  public JsonResult buyUseRight(Trade trade) {
    //  传saleId,buyId,txHash,status
    tradeService.buyUseRight(trade);

    return new JsonResult(OK);
  }

  /**
   * 购买所有权同时生成交易凭证
   * @param trade 交易信息
   * @param chainTime 上链时间
   * @param blockHeight 区块高度
   * @return json
   */
  @RequestMapping("buyOwnRight")
  public JsonResult buyOwnRight(Trade trade,String chainTime,String blockHeight) {
    //  传saleId,buyId,txHash
    tradeService.buyOwnRight(trade,chainTime,blockHeight);

    return new JsonResult(OK);
  }

  /**
   *获取全部交易
   * @return 交易列表
   */
  @RequestMapping("getAllTrade")
  public JsonResult getAllTrade() {
    List<TradeDTO> dtoList = tradeService.getAllTrade();
    JsonResult result = new JsonResult(OK);
    result.addData("tradeList",dtoList);
    return result;
  }

  /**
   * 今日收益
   * @param userId 用户id
   * @return json
   */
  @RequestMapping("getTodayEarning")
  public JsonResult getTodayEarning(Integer userId){
    Double earning = tradeService.getTodayEarning(userId);
    JsonResult result = new JsonResult(OK);
    result.addData("earning",earning);
    return result;
  }

  @RequestMapping("viewDetails")
  public JsonResult viewDetails(Integer tradeId,String chainTime,String blockHeight){
    tradeService.viewDetails(tradeId,chainTime,blockHeight);
    return new JsonResult(OK);
  }
}
